/**
 * Created by Administrator on 2017/7/17 0017.
 */
$(document).ready(function () {
        $('.deploy-install-tasktable').each(function () {
        var tablself = this;
        var trobj = $(tablself).find('tbody').children();
        for(i=0;i<trobj.length;i++){
            if(i==0){
                $(trobj[i]).children().eq(0).attr('rowspan',trobj.length)
            }else {
                $(trobj[i]).children().eq(0).remove();
            }

        }
    })
});



$('#deploy-install-addelement-savebtn').delegate('.btn-primary', 'click', function () {
    var csrftoken = $.cookie('csrftoken');
    var element_name = $('#deploy-install-addelement-name').val().trim();
    var element_humaname = $('#deploy-install-addelement-humanname').val().trim();
    var element_desc = $('#deploy-install-addelement-desc').val().trim();
    $.ajax({
        url: '/deploy/addelement/',
        type: 'POST',
        headers: {'X-CSRFToken': csrftoken},
        data: {'element_name': element_name, 'element_humaname': element_humaname, 'element_desc': element_desc},
        success: function (data) {
            console.log(data);
            $('#deploy-install-addelement-modal').css('display', 'none')
            $('.modal-backdrop').remove()

        }
    });

});

$('#deploy-install-addtask-savebtn').delegate('.btn-primary', 'click', function () {
    var csrftoken = $.cookie('csrftoken');
    var taskname = $('#deploy-install-task-name').val().trim();
    var taskdesc = $('#deploy-install-task-desc').val().trim();
    $.ajax({
        url: '/deploy/addtask/',
        type: 'POST',
        headers: {'X-CSRFToken': csrftoken},
        data: {'taskname': taskname, 'taskdesc': taskdesc},
        success: function (data) {
            console.log(data);
            $('#deploy-install-addtask-modal').css('display', 'none')
            $('.modal-backdrop').remove()
        }
    })
});

$('#deploy-install-addlayout-addrow').delegate('button', 'click', function () {
    var tagelementlist = [];
    var elementlists = [];
    $('#deploy-install-layout-checklist').find('input:checkbox').each(function () {
        var checkself = this;
        if ($(checkself).prop('checked') == true) {
            var element = $(checkself).next().text();
            var ele = $(checkself).val();
            tagelementlist.push(element);
            elementlists.push(ele);
        }
    });
    var tagtaskid = $('#deploy-install-task').val();
    $('#deploy-install-task option').each(function () {
        var opself = this;
        if($(opself).attr('value')==tagtaskid){
            taskname = $(opself).text();
        }
    });

    var taghostandgroup = $('.deploy-install-host-multiple').val();
    var h = $('.deploy-install-host-multiple').next().find('.select2-selection__choice').text();
    re = new RegExp('×', 'g');
    var hostgroupname = h.replace(re, ' ');

    var td1 = "<td hostandgroup='" + taghostandgroup + "'>" + hostgroupname + "</td>";
    var td2 = "<td taskid='" + tagtaskid + "'>" + taskname + "</td>";
    var td3 = "<td elementlists='" + elementlists + "'>" + tagelementlist + "</td>";
    var td4 = '<td><button class="demo-delete-row btn btn-danger btn-xs"><i class="demo-pli-cross"></i></button></td>'
    var newtr = document.createElement('tr');
    $(newtr).append(td2);
    $(newtr).append(td1);
    $(newtr).append(td3);
    $(newtr).append(td4);
    $('#deploy-install-addlayout-previewtable').append(newtr)

});

$('#deploy-install-addlayout-previewtable').delegate('.demo-delete-row', 'click', function () {
    var delself = this;
    $(delself).parent().parent().remove();
});


$('#deploy-install-layout-savebtn').delegate('.btn-primary', 'click', function () {
    var data = [];
    var csrftoken = $.cookie('csrftoken');
    $('#deploy-install-addlayout-previewtable tr').each(function () {
        var tdlist = {};
        var trself = this;
        var tdobj = $(trself).children();
        tdlist['taskid'] = $(tdobj).eq(0).attr('taskid');
        tdlist['hostandgroup'] = $(tdobj).eq(1).attr('hostandgroup');
        tdlist['elementlists'] = $(tdobj).eq(2).attr('elementlists');
        data.push(JSON.stringify(tdlist))
    });
    console.log(data);
    $.ajax({
        url: '/deploy/taskschedu/',
        type: 'POST',
        headers: {'X-CSRFToken': csrftoken},
        data: {'alldata':data},
        success: function (data) {
            console.log(data);
            $('#deploy-install-layout-modal').css('display', 'none');
            $('.modal-backdrop').remove()
        }
    });
});